package cn.hp.controller;


import cn.hp.entity.Menu;
import cn.hp.entity.User;
import cn.hp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;
import cn.hp.util.*;

import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @GetMapping("login")
    public String login(){
        return "login";
    }
    @GetMapping("main")
    public String main(){
        return "main";
    }
    @GetMapping("getUser")
    @ResponseBody
    //返回内容,如果返回的是页面,不用添加
    public Result getUser(HttpSession session){
        User user= (User) session.getAttribute("user");
        return Result.success(user);
    }
    @GetMapping("getUidMenu")
    @ResponseBody
    public Result getUidMenu(HttpSession session){
        User user= (User) session.getAttribute("user");
        List<Menu> list=userService.selectUidMenu(user);
        return Result.success(list);
    }
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "login";
    }
    //登录
    @PostMapping("dologin")
    @ResponseBody
    public Result dologin(@RequestBody User user, boolean rememberMe, HttpSession session) {
        boolean loginflag = false;
        System.out.println(user);
        User user1 = userService.dologin(user);
        if(user1!=null){
            session.setAttribute("user", user1);
            return Result.success();
        }else{
            return Result.error("账号密码错误！");
        }
    }

}
